﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using Unity.Mvc3;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace ClassLibrary1
{
    /// <summary>
    /// Dependency resolver with respect to BMW IS
    /// </summary>
    public static class BMWISDependencyResolver
    {
        #region Private Constants

        private const string UnitySectionName = "unity";
        private const string ContainerName = "container";

        #endregion
        #region Public Methods

        /// <summary>
        /// Initializes the dependency injection container.
        /// </summary>
        public static void InitializeDependencyInjectionContainer()
        {
            //UnityConfigurationSection unitySection = ConfigurationManager.GetSection(UnitySectionName) as UnityConfigurationSection;
            IUnityContainer container = new UnityContainer();
            container.LoadConfiguration(ContainerName);
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
        }

        /// <summary>
        /// Gets the service.
        /// </summary>
        /// <typeparam name="ServiceType">The type of the ervice type.</typeparam>
        /// <returns>Returns null if service is not found</returns>
        public static ServiceType GetService<ServiceType>()
        {
            ServiceType serviceType;
            try
            {
                serviceType = DependencyResolver.Current.GetService<ServiceType>();
            }
            catch
            {
                serviceType = default(ServiceType);
            }
            
            if (serviceType == null)
            {
                serviceType = ServiceLocator.Current.GetInstance<ServiceType>();
            }
            return serviceType;
        }

        /// <summary>
        /// Gets the services.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <returns></returns>
        public static IEnumerable<ServiceType> GetServices<ServiceType>()
        {
            IEnumerable<ServiceType> serviceTypes = null;
            try
            {
                serviceTypes = DependencyResolver.Current.GetServices<ServiceType>();
                // accessing count will throw exception if there was not instance found
                serviceTypes.Count();
            }
            catch
            {
                serviceTypes = null;
            }
            
            if (serviceTypes == null || serviceTypes.LongCount() <= 0)
            {
                serviceTypes = ServiceLocator.Current.GetAllInstances<ServiceType>();
            }
            return serviceTypes;
        }

        #endregion
    }
}
